/**
 *@description 用户画像》自然人属性》化妆习惯
 *@author yhw
 */
import {
  rateQuery,
  preferenceQuery,
  occasionQuery
} from '../services/PersonsCustom';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'PersonsCustom',

  state: {
    rateData: null,
    rateLoading: false,
    preferenceData: null,
    preferenceLoading: false,
    occasionData: null,
    occasionLoading: false,
  },

  subscriptions: {},

  effects: { * rateQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'rateLoading'
      });

      const {
        data
      } = yield call(rateQuery, parse(payload));

      if (data) {

        let temp = {
            legend: [],
            value: []
          },
          colors = ['#FFF1C7', '#B3A9A4', '#DDDAD6', '#F3F2F0', '#FFE5D2'];
        data.data && data.data.map(function(item, index) {
          //temp.legend.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'rateQuerySuccess',
          payload: {
            rateData: temp,
          },
        });
      } else {
        error("获取化妆频率失败");
      }
    },
    //===================================
    * preferenceQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'preferenceLoading'
      });

      const {
        data
      } = yield call(preferenceQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#C3C0B9', '#DAAF77', '#A8A49F'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'preferenceQuerySuccess',
          payload: {
            preferenceData: temp,
          },
        });
      } else {
        error("获取妆容偏好失败");
      }
    },
    //===================================
    * occasionQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'occasionLoading'
      });

      const {
        data
      } = yield call(occasionQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#C3C0B9', '#DAAF77', '#FFF1C7', '#FFCAA4', '#FFE5D2'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'occasionQuerySuccess',
          payload: {
            occasionData: temp,
          },
        });
      } else {
        error("获取化妆场合失败");
      }
    },
  },

  reducers: {
    rateLoading(state) {
      return {...state,
        rateLoading: true
      };
    },
    rateQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        rateLoading: false
      };
    },
    //===================================
    preferenceLoading(state) {
      return {...state,
        preferenceLoading: true
      };
    },
    preferenceQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        preferenceLoading: false
      };
    },
    //===================================
    occasionLoading(state) {
      return {...state,
        occasionLoading: true
      };
    },
    occasionQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        occasionLoading: false
      };
    },
  },

};